// 
// Copyright 2010 Classpath Pty Ltd <opensource@classpath.com.au>
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 

using build

**
** Tests the `PostgreSqlAdapter` class.
**
internal class PostgreSqlAdapterTest : Test {

    **
    ** Stores an instance of `PostgreSqlAdapter` for testing.
    **
    PostgreSqlAdapter? adapter

    **
    ** Stores an instance of `CanCreateColumn` for testing.
    **
    CanCreateColumn? canCreateColumn

    **
    ** Stores an instance of `CanUseColumnOptions` for testing.
    **
    CanUseColumnOptions? canUseColumnOptions

    **
    ** Performs additional setup needed for PostgreSqlAdapterTest
    **
    override Void setup() {
        super.setup()
        adapter = PostgreSqlAdapter()
        canCreateColumn = CanCreateColumnTestClass()
        canUseColumnOptions = CanUseColumnOptionsTestClass()
    }

    **
    ** Cleans up additional setup needed for PostgreSqlAdapterTest
    **
    override Void teardown() {
        super.teardown()
        adapter = null
        canCreateColumn = null
        canUseColumnOptions = null
    }
    
    **
    ** Tests `GenericAdapter.createColumnSql`
    **
    Void testCreateColumnSql() {
        verifyEq("column_name BLOB", adapter.createColumnSql(canCreateColumn.createColumn("column_name", canCreateColumn.blob)))
        verifyEq("ADD COLUMN column_name BLOB", adapter.createColumnSql(canCreateColumn.createColumn("column_name", canCreateColumn.blob), true))
        verifyEq("column_name BLOB", adapter.createColumnSql(canCreateColumn.createColumn("column_name", canCreateColumn.blob) { autoIncrement }))
        verifyEq("column_name BLOB", adapter.createColumnSql(canCreateColumn.createColumn("column_name", canCreateColumn.blob) { comment("comment") }))
        verifyEq("column_name BLOB", adapter.createColumnSql(canCreateColumn.createColumn("column_name", canCreateColumn.blob) { autoIncrement; comment("comment") }))
        verifyEq("column_name SERIAL", adapter.createColumnSql(canCreateColumn.createColumn("column_name", canCreateColumn.integer) { autoIncrement }))
    }

    **
    ** Tests `PostgreSqlAdapter.autoIncrementColumnOptionSql`
    **
    Void testAutoIncrementColumnOptionSql() {
        verifyEq("", adapter.autoIncrementColumnOptionSql(canUseColumnOptions.autoIncrement))
    }

    **
    ** Tests `PostgreSqlAdapter.commentColumnOptionSql`
    **
    Void testCommentColumnOptionSql() {
        verifyEq("", adapter.commentColumnOptionSql(canUseColumnOptions.comment("comment")))
    }

    **
    ** Tests `PostgreSqlAdapter.sql`.
    **
    Void testMigrationZero() {
        migrationZero := MigrationZero()
        migrationZero.up
        verifyEq(1, migrationZero.modifications.size)
        expected := """CREATE TABLE version_history (
                       vh_id SERIAL PRIMARY KEY,
                       vh_timestamp TIMESTAMP DEFAULT NOW() NOT NULL,
                       vh_from_version VARCHAR(32) NOT NULL,
                       vh_to_version VARCHAR(32) NOT NULL
                       )"""
        verifyEq(expected, adapter.sql(migrationZero.modifications[0]))
    }

}
